package com.platform.validator;

import com.platform.utils.RegExValidator;
import org.apache.commons.lang3.StringUtils;

import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
 * 身份证
 * @author Administrator
 */
@Target(ElementType.FIELD)
@Retention(RUNTIME)
@Documented
@Constraint(validatedBy = {IdCode.IdCardUtil.class})
public @interface IdCode {

    String regexp() default "";

    String message() default "身份证格式不正确";

    Class<?>[] groups() default { };

    Class<? extends Payload>[] payload() default { };

    class IdCardUtil implements ConstraintValidator<IdCode,String> {

        @Override
        public void initialize(IdCode constraintAnnotation) {
            ConstraintValidator.super.initialize(constraintAnnotation);
        }

        @Override
        public boolean isValid(String value, ConstraintValidatorContext context) {
            if(StringUtils.isBlank(value)){
                return true;
            }
            return RegExValidator.checkIdCodeForSys(value);
        }
    }
}
